<?php
class user extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->tit = '用户';
		$this->modelObj = D('sys_user');
	}
    
    //列表
	public function index(){
		/*分类切换*/
		$this->tabtit = array('全部','禁用');

		$cond = "1=1 ";

		//加入查询条件
		switch($this->tab){
			case '1':
				$cond .=" and status=0";
				break;
			default:
				$cond .=" and status=1";
		}
		//搜索
		if($this->keyword){
			$cond.=" and (nickName like '%".$this->keyword."%') ";
		}

		$result = $this->modelObj->get_pagelist($cond,'','*',10);
        if($result){
        	// $this->roles = $this->roleObj->ids($result['list'],'role_id');

        	$this->list = $result['list'];
			$this->showPage = $result['showPage'];
			$this->total = $result['total'];
		}
		
	}


	//充值
	public function recharge()
	{

		// $this->choujiangObj = D('choujiang');
		$id = arg('id');
		if($this->isAjax()){
			$recharge = $this->postData('recharge');
			$source = arg('source');
			$user_id = arg('user_id');

			$choujiang = $recharge['choujiang'];
			if($choujiang['value']>0){
				// $mode = $choujiang['mode'];
				// $score = $choujiang['value'];
				$remark = $choujiang['remark'] ?? '管理员后台操作';

				switch($choujiang['mode']){
					case 'inc':
						// $this->modelObj->incr(array('id'=>$user_id),'score',$score);
						//增加积分
						$this->modelObj->add_choujiang($user_id,$choujiang['value'],$remark);
						break;
					case 'dec':
						// $this->modelObj->decr(array('id'=>$user_id),'score',$score);
						//减少积分
						$this->modelObj->add_choujiang($user_id,-$choujiang['value'],$remark);
						break;
					case 'final':
						$arrBase = array(
							'choujiang' => $choujiang['value'],
						);
						$this->modelObj->edit($user_id,$arrBase);
						//增加积分
						$this->modelObj->add_choujiang($user_id,0,'后台抽奖次数重置');
						break;
				}
			}

			$points = $recharge['points'];
			if($points['value']>0){
				// $mode = $points['mode'];
				// $value = $points['value'];
				$remark = $points['remark'] ?? '管理员后台操作';

				switch($points['mode']){
					case 'inc':
						// $this->modelObj->incr(array('id'=>$user_id),'score',$score);
						//增加积分
						$this->modelObj->add_score($user_id,$points['value'],$remark);
						break;
					case 'dec':
						// $this->modelObj->decr(array('id'=>$user_id),'score',$score);
						//减少积分
						$this->modelObj->add_score($user_id,-$points['value'],$remark);
						break;
					case 'final':
						$arrBase = array(
							'score' => $points['value'],
						);
						$this->modelObj->edit($user_id,$arrBase);
						//增加积分
						$this->modelObj->add_score($user_id,0,'后台积分重置');
						break;
				}
			}
			
			$this->renderSuccess('充值成功');
		}
	}
	
}